<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Broker;
use App\Models\Machine;
use App\Models\MachineManager;
use App\Models\Role;
use App\Models\Auth;
use App\Http\Requests\Admin\AdminUserRequest;

class BrokerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Broker $broker)
    {
        $manager = auth('admin')->user();
        if($manager->type == 2){
            $broker = $broker->where(['parent_id' => $manager->id]);
        }
        $admin_users = $broker->filter($request->all())->orderBy('id', 'desc')->paginate(config('api.page.limit'));
        $name = $request->name;
        $phone = $request->phone;
        return view('admins.broker.index',compact('admin_users','name', 'phone'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Broker $broker)
    {
        $manager = auth('admin')->user();
        if($manager->type == 2){
            $broker = $broker->where('id',$manager->id);
        }    
        $brokers = $broker->get();    
        return view('admins.broker.add', compact('brokers'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminUserRequest $request, Broker $broker)
    {
        $result = $broker->create($request->all());
        if(!$result){
            return back()->with('warning','添加失败');
        }
        return redirect()->route('admin.brokers.index')->with('success', '添加成功');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Broker $broker)
    {
        $brokers = new Broker;
        $manager = auth('admin')->user();
        if($manager->type == 2){
            $brokers = $brokers->where('id',$manager->id);
        }    
        $brokers = $brokers->get(); 
        return view('admins.broker.edit', compact('broker', 'brokers'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminUserRequest $request, Broker $broker)
    {
        $result = $broker->update($request->all());
        if(!$result){
            return back()->with('warning','编辑失败');
        }
        return redirect()->route('admin.brokers.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // public function destroy($id, AdminUserRequest $request)
    // {
    //     dd(234234);
    // }
    public function delete(Request $request, Auth $auth, Broker $broker)
    {
        $manager = $auth->where('parent_id', $request->id)->first();
        $broker = $broker->with('machine')->find($request->id);
        if($manager || $broker->machine->isNotEmpty()){
            return json_encode(['code' => 400, 'message' => '该账号下存在下级账户或设备,请移除后再删除']);
        }
        $broker->delete();
        return json_encode(['code' => 200, 'message' => '删除成功过']);
    }

    public function resetPassword(Request $request, Broker $broker)
    {
        $broker = $broker->find($request->id);
        $broker->password = encrypt_str('123456');
        $res = $broker->save();
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $res]);
    }


    //获取json经销商
    public function list(Request $request, Auth $auth, Broker $broker)
    {
        $user = auth('admin')->user();
        if($user->type == 2){
            $broker = $broker->where('parent_id',$user->id)->orWhere('id', $user->id);
            
        }
        $brokers = $broker->get();
        return json_encode(['code' => 200, 'msg' => 'success', 'data' => $brokers]);
    }
    
}
